package com.tianxin.workbao.workBao.Activity;

import android.content.Context;
import android.os.Bundle;
import androidx.annotation.Nullable;
import com.google.android.material.floatingactionbutton.FloatingActionButton;
import androidx.appcompat.content.res.AppCompatResources;
import androidx.appcompat.widget.Toolbar;
import android.text.Editable;
import android.text.TextWatcher;
import android.util.AttributeSet;
import android.view.View;
import android.widget.EditText;
import android.widget.TextView;
import android.widget.Toast;

import com.tianxin.workbao.workBao.R;
import com.tianxin.workbao.workBao.bean.Fueldiary;
import com.tianxin.workbao.workBao.common.Constant;
import com.tianxin.workbao.workBao.dataProvide.OilPriceProvide;
import com.tianxin.workbao.workBao.listener.UpdateListViewListener;
import com.tianxin.workbao.workBao.utils.common.SPUtils;
import com.tianxin.workbao.workBao.utils.common.TimeUtils;

import java.util.Date;

/**
 * Created by hacker on 2017/6/22.
 */

public class AddOilOauthActivity extends BaseActivity implements View.OnClickListener, UpdateListViewListener {

    private TextView carInformationTV;
    private TextView addTimeTV;
    private TextView oauthTimeTV;
    private TextView addUserTV;
    private TextView totalMoneyTV;
    private EditText oilMassET;
    private TextView unitPriceTV;
    private TextView numberTV;
    private TextView addMoneyTV;
    private TextView addOilMassTV;


    private FloatingActionButton saveFab;
    private Fueldiary mFueldiary;

    private OilPriceProvide oilPriceProvide;

    private Float volume;
    private Float money;


    @Override
    protected void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.add_oil_oauth);
        super.initStatusBar(true);
        initData();

        initView();

        initEvent();

        bindView();
    }

    @Override
    public View onCreateView(String name, Context context, AttributeSet attrs) {
        return super.onCreateView(name, context, attrs);
    }

    public void initView() {
        mToolbar = (Toolbar) findViewById(R.id.toolbar);
        mToolbar.setTitle(R.string.add_oil_oauth);
        setSupportActionBar(mToolbar);
        mToolbar.setNavigationIcon(AppCompatResources.getDrawable(this, R.drawable.ic_action_back_24dp));
        saveFab = (FloatingActionButton) findViewById(R.id.save_fab);

        carInformationTV = (TextView) findViewById(R.id.car_information);
        addTimeTV = (TextView) findViewById(R.id.add_time);
        oauthTimeTV = (TextView) findViewById(R.id.oauth_time);
        addUserTV = (TextView) findViewById(R.id.add_user);
        totalMoneyTV = (TextView) findViewById(R.id.total_money);
        addMoneyTV = (TextView) findViewById(R.id.add_money);
        oilMassET = (EditText) findViewById(R.id.oil_mass);
        unitPriceTV = (TextView) findViewById(R.id.unit_price);
        numberTV = (TextView) findViewById(R.id.number);
        addOilMassTV = (TextView) findViewById(R.id.add_oil_mass);
    }

    @Override
    public void initData() {
        Bundle bundle = getIntent().getExtras();
        mFueldiary = (Fueldiary) bundle.getSerializable("fueldiary");
    }


    @Override
    public void initEvent() {
        //navigationIcon event
        mToolbar.setNavigationOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                AddOilOauthActivity.this.finish();
            }
        });
        saveFab.setOnClickListener(this);

        oilMassET.addTextChangedListener(new TextWatcher() {
            @Override
            public void beforeTextChanged(CharSequence charSequence, int i, int i1, int i2) {

            }

            @Override
            public void onTextChanged(CharSequence charSequence, int i, int i1, int i2) {

            }

            @Override
            public void afterTextChanged(Editable editable) {
                String volumeString = editable.toString();
                if (volumeString.length() == 0) {
                    return;
                }
                try {
                    volume = Float.valueOf(volumeString);
                    if (volume > mFueldiary.getOilmass()) {
                        showMessage("实加升数不能大于开单升数");
                        oilMassET.setText("");
                        return;
                    } else {
                        money = Float.valueOf(Constant.mDecimalFormat.format(volume * mFueldiary.getUnitprice()));
                    }
                    totalMoneyTV.setText(money+"");
                } catch (NumberFormatException e) {
                    showMessage("实加升数输入错误");
                    oilMassET.setText("");
                    e.printStackTrace();
                    return;
                }
            }
        });
    }

    @Override
    public void bindView() {
        numberTV.setText(mFueldiary.getNumber());
        addUserTV.setText(SPUtils.getPrefString(Constant.CURRENT_USER_NAME, "null"));
        addTimeTV.setText(TimeUtils.parseDateTime(mFueldiary.getAddtime()));
        oauthTimeTV.setText(TimeUtils.parseDateTime(new Date()));
        carInformationTV.setText(mFueldiary.getPlatenumbers());

        unitPriceTV.setText(mFueldiary.getUnitprice() + "");
        addOilMassTV.setText(mFueldiary.getOilmass() + "");
        addMoneyTV.setText(mFueldiary.getTotalmoney() + "");
    }


    @Override
    public void onBackPressed() {
        //alert
        super.onBackPressed();
    }

    @Override
    public void onClick(View view) {
        int viewId = view.getId();
        switch (viewId) {
            case R.id.save_fab:
                oauthRecord();
                break;
        }
    }

    /**
     * add oil
     */
    private void oauthRecord() {

        if (volume!=null && volume > 0 && volume < mFueldiary.getOilmass()) {
            mFueldiary.setIsoauth(1);
            try {
                mFueldiary.setOauthuserid((int) SPUtils.getPrefLong(Constant.CURRENT_USER_ID, 0));
                mFueldiary.setOauthusername(SPUtils.getPrefString(Constant.CURRENT_USER_NAME, "no"));
            } catch (Exception e) {
                // user no login
                e.printStackTrace();
            }
            mFueldiary.setOauthtime(new Date());
            mFueldiary.setTotalmoney(money);
            mFueldiary.setOilmass(volume);
            getOilPriceProvide().OauthFueldiary(mFueldiary);

        } else {
            showMessage("实加升数不能大于开单升数");
        }
    }

    @Override
    public void showMessage(String msg) {
        if (msg == "ok") {
            this.finish();
        } else if (msg.contains("failure")) {
            Toast.makeText(this, "审核失败！\n " + msg.substring(msg.indexOf("failure") + 1, msg.length()), Toast.LENGTH_LONG).show();
        } else {
            Toast.makeText(this, msg, Toast.LENGTH_LONG).show();
        }
    }

    @Override
    public void showProgress() {
        getSweetAlertDialog(this).showDialog("",0);
    }

    @Override
    public void hideProgress() {
        getSweetAlertDialog(this).dismiss();
    }

    @Override
    public void onRefreshData(Object result) {

    }

    @Override
    public void onLoadMoreData(Object result) {

    }

    public OilPriceProvide getOilPriceProvide() {
        if (oilPriceProvide == null) {
            oilPriceProvide = new OilPriceProvide(this, this);
        }
        return oilPriceProvide;
    }
}
